define(function(require, exports) {
	var c1 = require('c1');

	var c1NewPage = null;

	var c1EditPage = null;

	var c1Page = {};

	c1Page = {
		init : function() {
			$(document).on("click", 'form[name="navigationForm"] button.c1-query', c1Page.queryClick);

			c1.form.pagination({
				paginationSelector : 'div.c1-pagination-load',
				paginationNoSelector : 'form[name="navigationForm"] input[name="paginationNo"]',
				loadPagination : c1Page.loadPagination
			});

			$(document).on("click", '.c1-operation-delete-all', c1Page.deleteAllClick);

			$(document).on("click", '.c1-operation-new', c1Page.newClick);

			$(document).on("click", '.c1-operation-edit', c1Page.editClick);

			$(document).on("click", '.c1-operation-delete-row', c1Page.deleteRowClick);

			c1.form.joinCheckboxs({
				mainSelector : 'input[name="checkboxIds"][type="checkbox"]',
				childSelector : 'input[name="id"][type="checkbox"]'
			});
		},
		queryClick : function() {
			$('form[name="navigationForm"] input[name="paginationNo"]').val(1);

			c1Page.loadPagination();
		},
		loadPagination : function() {
			$('.c1-modal').find('.modal-body').html('');

			var url = cardone.webContextUrl + '/authority/navigation/pagination.html';

			var data = c1.form.serializeJson('form[name="navigationForm"]');

			c1.message.progress();

			$("div.c1-pagination-load").load(url, data, function() {
				c1.message.progress('close');

				$(this).show();

				var $table = $(this).find('table');

				if ($table.size() < 1) {
					$('.c1-operation-btn').hide();

					return;
				}

				$('.c1-operation-btn').show();
			});
		},
		deleteAllClick : function() {
			var $parentIds = $('input[name="id"]:checked');

			if ($parentIds.size() < 1) {
				c1.message.alert({
					title : '提示消息',
					message : '请选择需删除的数据！'
				});

				return;
			}

			var ids = $parentIds.map(function() {
				return $(this).val();
			}).get().join(", ");

			c1Page.deleteRow(ids);
		},
		newClick : function() {
			c1.message.progress();

			var url = cardone.webContextUrl + '/authority/navigation/new.html';

			var $c1Modal = $('.c1-modal');

			$c1Modal.find('.modal-body').load(url, function() {
				c1.message.progress('close');

				require.async([ './new' ], function(page) {
					if (c1NewPage == null) {
						c1NewPage = page;
					}

					c1NewPage.init(c1Page.loadPagination);

					$c1Modal.find('.modal-title').html('导航新增');

					$c1Modal.modal('show');
				});
			});
		},
		editClick : function() {
			c1.message.progress();

			var url = cardone.webContextUrl + '/authority/navigation/edit.html';

			var $c1Modal = $('.c1-modal');

			$c1Modal.find('.modal-body').load(url, {
				'id' : $(this).closest('tr').attr('navigationId')
			}, function() {
				c1.message.progress('close');

				require.async([ './edit' ], function(page) {
					if (c1EditPage == null) {
						c1EditPage = page;
					}

					c1EditPage.init(c1Page.loadPagination);

					$c1Modal.find('.modal-title').html('导航编辑');

					$c1Modal.modal('show');
				});
			});
		},
		deleteRow : function(ids) {
			c1.message.confirm({
				title : '确认',
				message : '将删除选择的数据?',
				fn : function(r) {
					if (!r) {
						return;
					}

					var url = cardone.webContextUrl + '/authority/navigation/delete.json';

					c1.message.progress();

					$.post(url, {
						'ids' : ids
					}, function(data) {
						c1.message.progress('close');

						if (data.code == '0') {
							c1.message.alert({
								title : '提示消息',
								message : '删除成功',
								fn : c1Page.loadPagination
							});

							return;
						}

						c1.message.alert({
							title : '提示消息',
							message : '删除失败:' + data.message,
							fn : c1Page.loadPagination
						});
					});
				}
			});
		},
		deleteRowClick : function() {
			var parentId = $(this).closest('tr').attr('navigationId');

			c1Page.deleteRow(parentId);
		}
	};

	require('bootstrap');
	require('bootstrap-typeahead');

	return {
		init : c1Page.init
	};
});